package com.coolwen.controller;

import com.coolwen.entity.User;
import com.coolwen.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.SessionScope;


/**
 * Created by user on 2016-11-23.
 */
@Controller
@SessionAttributes("loginUser")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "login-post",method = RequestMethod.POST)
    public String login(Model model,@RequestParam String openId,@RequestParam String userPassword){
        User loginUser = userService.findByUserOpenIdAndUserPassword(openId,userPassword);
        if(loginUser==null){
            return "user/login";
        }else {
            model.addAttribute("loginUser",loginUser);
            return "redirect:/login-user/index";
        }
    }

    @RequestMapping("login-page")
    public String loginPage(){
        return "user/login";
    }

    @RequestMapping("login-user/logout")
    public String logout(@ModelAttribute("loginUser") User loginUser,SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "redirect:/login-user/index";
    }
}
